package com.thti.mecool.client.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.thti.mecool.client.util.Util;

/**
 * 
 * @author weiguo.ren
 * 
 */

public class ConnectivityReceiver extends BroadcastReceiver {

   

    private NotificationService notificationService;

    public ConnectivityReceiver(NotificationService notificationService) {
        this.notificationService = notificationService;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Util.print( "ConnectivityReceiver.onReceive()...");
        String action = intent.getAction();
        Util.print( "action=" + action);

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
     
        if (networkInfo != null) {
            Util.print( "Network Type  = " + networkInfo.getTypeName());
            Util.print( "Network State = " + networkInfo.getState());
            if (networkInfo.isConnected()) {
                Util.print( "Network connected");
                notificationService.connect();
            }
        } else {
            Util.print( "Network unavailable");
            notificationService.disconnect();
        }
    }

}
